//
//  ELHomeViewController.swift
//  SinaWeiBo
//
//  Created by jiwang on 15/12/15.
//  Copyright © 2015年 jiwang. All rights reserved.
//

import UIKit

class ELHomeViewController: ELBasicViewController {
    private let ReuseIdentifier = "ReuseIdentifier"

    private var statuses : [ELStatus]? {
        didSet {
            //控制器刷新
            self.tableView.reloadData()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //判断用户是否登录
        if !ELUserAccount.hasUserInfo {
            //没有则返回
            return
        }
        
        //设置导航栏按钮
        setupNavBarButtonView()
        
        //注册cell
        tableView.registerClass(ELHomeTableViewCell.self, forCellReuseIdentifier: ReuseIdentifier)
        
        //设置行高
        //        tableView.rowHeight = 100
        tableView.estimatedRowHeight = 200
//        tableView.rowHeight = UITableViewAutomaticDimension
        
        //取消分割线
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None
        
        //读取用户数据
        ELStatus.loadUserStatuses { (list, error) -> () in
            
            //将返回的数组赋值给控制器属性
            self.statuses = list
            
        }
    }
    
    ///设置导航栏按钮
    private func setupNavBarButtonView () {
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendsearch")
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop")
        
        //两个问号表示如果没有值就赋值后面的
        let title = ELUserAccount.loadUserAccount()?.screen_name ?? "读取用户名失败"
        
        let button = ELHomeTitleButton.creatWith(title, imageName: "navigationbar_arrow_down")
        navigationItem.titleView = button
        ///为标题添加点击事件
        button.addTarget(self, action: "rotateToPopoView:", forControlEvents: UIControlEvents.TouchUpInside)
    }
    
    ///旋转图片
    @objc private func rotateToPopoView (button:UIButton) {
        
        button.selected = !button.selected
        
        UIView.animateWithDuration(0.5) { () -> Void in
            button.imageView?.transform = button.selected ? CGAffineTransformMakeRotation(CGFloat(M_PI) - 0.000001) : CGAffineTransformIdentity
        }
    }
    
    // MARK: - 数据源方法

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       
        return statuses?.count ?? 0
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(ReuseIdentifier)! as! ELHomeTableViewCell
        
        let status = self.statuses![indexPath.row]
        
        cell.status = status
        
        return cell
    }
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        //取出模型
        let status = self.statuses![indexPath.row]
        
        //有缓存行高的话返回缓存
        if status.RowHeight != nil {
//            print("\t缓存行高 = \(status.RowHeight)")
            return status.RowHeight!
        }
        
        //没有缓存则再计算行高
        let cell = tableView.dequeueReusableCellWithIdentifier(ReuseIdentifier) as! ELHomeTableViewCell
        
        let height = cell.rowHeight(status)
//        print("\t计算行高 = \(height)")
        
        status.RowHeight = height
        
        return height
    }
}
